package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import sun.java2d.pipe.ValidatePipe;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //保存会员信息
    public void add(Member member) {
        String password = member.getPassword();
        if(password != null){
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {//2018.05
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }


    public Map findMemberCountBySelfTotal(Map dateArr) throws Exception {

        String startDate = (String) dateArr.get("start");
        String endDate = (String) dateArr.get("end");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //获取Date类型的起始和结束时间
        Date start = format.parse(startDate);
        Date end = format.parse(endDate);

        Map<String, Object> result = new HashMap<>();
        List<String> months = new ArrayList();
        List<Integer> memberCount = new ArrayList();


        Calendar calendarStart = Calendar.getInstance();//获取Calender实例
        Calendar calendarEnd = Calendar.getInstance();
        calendarStart.setTime(start);//获取Calendar类型的起始时间
        calendarEnd.setTime(end);//获取Calendar类型的结束时间
        //获取查询时间前的初始会员数
        Integer initCount = memberDao.findMemberCountBeforeDateTime(start);

        while (calendarStart.getTimeInMillis() <= calendarEnd.getTimeInMillis()) {
            //保存每次起始查询时间的毫秒值
            long timeInMillis = calendarStart.getTimeInMillis();
            //获取下月1号
            calendarStart.add(Calendar.MONTH, 1);
            calendarStart.set(Calendar.DAY_OF_MONTH, 1);
            //如果下月1号比结束查询时间早,则查询下月1号前的总会员数减去前一个数值的结果
            if (calendarStart.getTimeInMillis() < calendarEnd.getTimeInMillis()) {
                Integer countTemp = memberDao.findMemberCountBeforeDateTime(calendarStart.getTime());
                memberCount.add(countTemp - initCount);
                //更新之前查询总会员数总和
                initCount=countTemp;
                months.add(new SimpleDateFormat("yyyy.MM").format(new Date(timeInMillis)));
            } else {
                //当前查询时间和结束时间在同一个月
                Map<String, Date> mapTemp = new HashMap<>();//封装between and的参数
                Calendar newCalender = Calendar.getInstance();
                newCalender.setTime(calendarStart.getTime());
                newCalender.add(Calendar.MONTH, -1);
                newCalender.set(Calendar.DAY_OF_MONTH, 1);
                //判断本月初时间小于等于真实起始时间,则采用最开始的起始时间,反之,采用本月初作为起始时间
                if(newCalender.getTimeInMillis()>timeInMillis){
                    mapTemp.put("time1", calendarStart.getTime());
                } else{
                    mapTemp.put("time1", new Date(timeInMillis));
                }
                mapTemp.put("time2", calendarEnd.getTime());
                //根据起始和结束时间查询新增会员数
                Integer countTemp = memberDao.findMemberCountBetweenDate(mapTemp);
                //添加月份展示
                months.add(new SimpleDateFormat("yyyy.MM").format(new Date(timeInMillis)));
                //添加数据系列
                memberCount.add(countTemp);
            }
        }
        result.put("months", months);
        result.put("memberCount", memberCount);
        return result;
    }

}
